package com.cars.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.cars.dao.util.ConectionManager;
import com.cars.dto.Customer;

public class CustomerDao {

	public Customer loadCustomer(String userName, String password) {

		ResultSet resultSet = null;
		Customer custmomer = null;
		Connection connection = null;
		Statement statement = null;
		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			// Setup the connection with the DB
			connection = ConectionManager.getInstance().getConnection();

			// Statements allow to issue SQL queries to the database
			statement = connection.createStatement();
			// Result set get the result of the SQL query
			resultSet = statement
					.executeQuery("select * from customers where loginid ='"
							+ userName + "' and password='" + password + "'");
			if (resultSet == null) {
				return null;
			}

			while (resultSet.next()) {
				custmomer = new Customer();
				custmomer.setCustomerNumber(resultSet.getInt("customerNumber"));
				custmomer.setAddressLine1(resultSet.getString("addressLine1"));
				custmomer.setContactFirstName(resultSet
						.getString("contactFirstName"));
				custmomer.setContactFirstName(resultSet
						.getString("contactLastName"));
				custmomer.setCustomerName(resultSet.getString("customerName"));

				break;

			}

		} catch (Exception exception) {
			exception.printStackTrace();

		} finally {
			try {

				if (resultSet != null) {
					resultSet.close();
				}

				if (statement != null) {
					statement.close();

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		return custmomer;

	}

}
